package com.zhoug.commonlib2.notifycation;

import android.content.Context;
import android.media.AudioAttributes;
import android.media.MediaPlayer;
import android.net.Uri;

import com.zhoug.common2.utils.ResourceUtils;
import com.zhoug.logger2.Logger;


/**
 * 报警铃声
 *
 * @Author: zhoug
 * @Date: 2024-10-18
 * @Description:
 */
public class WarningRingHelper {
    private static final String TAG = ">>>WarningRingHelper";
    private MediaPlayer mMediaPlayer;//播放器
    private Uri mRingUri;//报警铃声uri
    private boolean _PendingStart = false;//Prepared成功后自动开始播放
    private static WarningRingHelper warningRingHelper;

    private WarningRingHelper() {

    }

    public static WarningRingHelper getInstance() {
        if (warningRingHelper == null) {
            synchronized (WarningRingHelper.class) {
                if (warningRingHelper == null) {
                    warningRingHelper = new WarningRingHelper();
                }
            }
        }
        return warningRingHelper;
    }


    /**
     * 初始化
     */
    private void initMediaPlayer() {
        if (mMediaPlayer == null) {
            mMediaPlayer = new MediaPlayer();
            mMediaPlayer.setAudioAttributes(new AudioAttributes.Builder()
                    .setUsage(AudioAttributes.USAGE_ALARM)
                    .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
                    .build());
            mMediaPlayer.setLooping(true); // 设置播放为循环模式
            mMediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
                @Override
                public void onPrepared(MediaPlayer mp) {
                    Logger.d(TAG, "onPrepared:");
                }
            });
            mMediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
                @Override
                public void onPrepared(MediaPlayer mp) {
                    if (_PendingStart && mMediaPlayer != null) {
                        _PendingStart = false;
                        mMediaPlayer.start();
                    }
                }
            });
        }
    }

    public void setRingUri(Uri ringUri) {
        this.mRingUri = ringUri;
    }

    public boolean isPlaying() {
        try {
            return mMediaPlayer != null && mMediaPlayer.isPlaying();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    /**
     * 开始报警
     */
    public void startAlarm(Context context) {
        initMediaPlayer();
        try {
            if (mRingUri == null) {
                mRingUri = ResourceUtils.getRawUri(context, "ring_warning");
            }
            mMediaPlayer.setDataSource(context, mRingUri);
            _PendingStart = true;
            mMediaPlayer.prepareAsync(); // 准备播放
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 停止报警
     */
    public void stopAlarm() {
        if (mMediaPlayer != null) {
            try {
                _PendingStart = false;
                mMediaPlayer.stop(); // 停止播放
                mMediaPlayer.release(); // 释放资源
                mMediaPlayer = null;
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }


}
